package com.javasm.config;

import com.javasm.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;

/**
 * @Author：MoDebing
 * @Version：1.0
 * @Date：2022-09-04-20:04
 * @Description:授权服务器
 */
@Configuration
@EnableAuthorizationServer
public class AuthonrizationServiceConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserServiceImpl userService;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("modebing")
                //配置client_secret
                .secret(passwordEncoder.encode("123456"))
                // 配置访问有效期
                .accessTokenValiditySeconds(3600)
                //重定向地址
                .redirectUris("https://www.baidu.com")
                .scopes("all")

                // 代表四种类型
                .authorizedGrantTypes("client_credentials", "refresh_token", "password", "authorization_code", "implicit");

    }


    /**
     * 密码模式要重写该方法 使用密码模式需要重写该方法
     *
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints.authenticationManager(authenticationManager) //
                .userDetailsService(userService);
    }
}
